package cn.edu.gdgm.system.utils.mqtt.client;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

/**
 * @author quan
 * @date 2021-03-07
 */
@Component
@Slf4j
public class MessageCallback implements MqttCallback {

    /**
     * 丢失对服务器端的连接后触发的回调
     * @param cause cause
     */
    @Override
    public void connectionLost(Throwable cause) {

    }

    /**
     * 订阅消息到达触发的回调
     * @param topic 主题
     * @param message msg
     * @throws Exception except
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("订阅消息：topic:{}, msgId:{}, qos:{}, payload:{}", topic, message.getId(),
                message.getQos(), new String(message.getPayload()));
    }

    /**
     * 发布消息完成回调
     * @param token token
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        int messageId = token.getMessageId();
        String[] topics = token.getTopics();
        log.info("发布消息完成， msgId:{}, 主题:{}", messageId, topics);
    }
}
